﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class PaymentListControl : RSBaseUserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnAddPayment_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int invoiceid = Convert.ToInt32(hfInvoiceId.Value);
                ucModalManagePaymentControl.ShowModal(userIdentity.RSCompanyId, invoiceid, 0);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void gvPayment_SelectedIndexChanged(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int paymentid = Convert.ToInt32(gvPayment.SelectedValue.ToString());
            int invoiceid = Convert.ToInt32(hfInvoiceId.Value);
            ucModalManagePaymentControl.ShowModal(companyid, invoiceid, paymentid);
        }

        public void LoadInvoicePaymentList(int companyid, int invoiceid)
        {
            hfInvoiceId.Value = invoiceid.ToString();

            SqlCommand myCommand;
            String sqlSELECT = "SELECT PaymentId,InvoiceId,CompanyId,LocationId,PaymentAmount,PaymentDT, (select t.typename from rs_tblType t where t.typeid=PaymentTypeId) as PaymentType FROM rs_tblPayment WHERE InvoiceId=@InvoiceId AND CompanyId=@CompanyId ORDER BY PaymentDT DESC";

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@invoiceid";
            param.Value = invoiceid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvPayment.DataSource = ds;
            gvPayment.DataBind();
            //lblCallCount.Text = gvPayment.Rows.Count.ToString();
            myConnection.Close();
        }

    }

    
}